﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.City6473.Web.Data.Repositories;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http;


namespace SpaceNeedle.City6473.WebSite.Controllers
{
    [RoutePrefix("api/me")]
    public class MeController : ApiController
    {
        private UserRepository userRepository = new UserRepository();
        private UserDeviceRepository userDeviceRepository = new UserDeviceRepository();

        // GET: Me
        public Me Get(string Id)
        {
            Guid id = new Guid(Id);

            var user = userRepository.GetById(id);
            if(user == null)
            {
                return null;
            }

            Me me = new Me();

            me.Id = user.Id;
            me.Name = user.Name;
            me.Phone = user.Phone;
            me.Picture = user.Picture;
            me.DeviceId = user.DeviceId;

            return me;
        }

        [HttpPost]
        [Route("{userId}/push/wp8/{pushId}/{deviceId}")]
        public bool AddWP8PushId(Guid userId, string pushId, string deviceId)
        {
            byte[] bytes = Convert.FromBase64String(pushId);
            pushId = Encoding.UTF8.GetString(bytes);

            bytes = Convert.FromBase64String(deviceId);
            deviceId = Encoding.UTF8.GetString(bytes);

            return this.AddPushId(userId, DevicePlatform.WP8, pushId, deviceId);
        }

        [HttpPost]
        [Route("{userId}/push")]
        public bool AddPushId(Guid userId, [FromBody]DeviceInfo info)
        {
            //byte[] bytes = Convert.FromBase64String(pushId);
            //pushId = Encoding.UTF8.GetString(bytes);

            //bytes = Convert.FromBase64String(deviceId);
            //deviceId = Encoding.UTF8.GetString(bytes);

            return this.AddPushId(userId, (DevicePlatform)Enum.Parse(typeof(DevicePlatform),info.Platform), info.PushId, info.DeviceId);
        }

        [HttpPost]
        [Route("{userId}/push/ios/{pushId}/{deviceId}")]
        public bool AddIOSPushId(Guid userId, string pushId, string deviceId)
        {
            return this.AddPushId(userId, DevicePlatform.IOS, pushId, deviceId);
        }

        [HttpPost]
        [Route("{userId}/push/android/{pushId}/{deviceId}")]
        public bool AddAndroidPushId(Guid userId, string pushId, string deviceId)
        {
            return this.AddPushId(userId, DevicePlatform.Android, pushId, deviceId);
        }

        [HttpPost]
        [Route("{userId}/push/w8/{pushId}/{deviceId}")]
        public bool AddW8PushId(Guid userId, string pushId, string deviceId)
        {
            return this.AddPushId(userId, DevicePlatform.W8, pushId, deviceId);
        }

        private bool AddPushId(Guid userId, DevicePlatform platform, string pushId, string deviceId)
        {
            try
            {
                var ud = userDeviceRepository.GetByUserAndPlaftorm(userId, platform);

                if (ud != null)
                {
                    ud.PushId = pushId;
                    ud.DeviceId = deviceId;
                    userDeviceRepository.Update(ud);
                }
                else
                {
                    ud = new UserDevice();
                    ud.Id = Guid.NewGuid();
                    ud.Platform = (int)platform;
                    ud.PushId = pushId;
                    ud.UserId = userId;
                    ud.DeviceId = deviceId;

                    userDeviceRepository.Insert(ud);
                }

                return true;
            }
            catch(DbEntityValidationException dbex) {

                int a = 0;
            }

            return false;
        }
    }
}
